package com.memoryangelapplication;

import java.util.ArrayList;

import com.memoryangelapplication.model.EventObj;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.os.PowerManager.WakeLock;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class AlertMessagePreview extends Activity  implements
TextToSpeech.OnInitListener {
	// Declare Variables
	ViewPager viewPager;
	AlertMessagePreviewAdapter adapter;
	String eventTimeStamp;
	
	//Display Variables
	String[] title;  // Index(3)
	String[] description;  // Index(4)
	String[] reminder_date;  // Index(6)
	String[] event_count_flag; 
	
	//Handling Variables
	String[] event_id; // Index(0)
	String[] event_alam_time; // Index(8)
	String[] event_url; // Index(11)
	String[] event_package; // Index(12)
	String[] event_ack_count; // Index(15)
	String[] eventReminderTime; // Index(16)
	String[] eventReminderNumber; // Index(17)
	String[] eventWaitTime; // Index(18)
	String[] eventNotifyType; // Index(19)
	
	int[] flag;
	int totEvents = 0;
	public static int counter = 0;
	private SQLiteDatabase mydb;
	private static final String DATABASE_NAME = "memoryangel.db";
	private static final String TAG = null;
	Context context;
	ArrayList<String> resultTitle = new ArrayList<String>();
	ArrayList<String> arFOrdTaxDt = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Remove title bar
	    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	    //Remove notification bar
	    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.alert_message_preview);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		
		getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		
		KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);  
		KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 
		lock.disableKeyguard();
		
		context = AlertMessagePreview.this;
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
		wakeLock.acquire();
		
		
		
		try{
			Vibrator myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
			myVib.vibrate(3000);
		}
		catch(Exception e){
			
		}
	
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			eventTimeStamp = bundle.getString("eventTimeStamp");
			mydb = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
			ContentValues newrowfDealer = new ContentValues();
			Cursor fdealerrows = null;
			try {
				String query = "SELECT * FROM table_events WHERE eventTimeStamp = '" + eventTimeStamp + "' AND event_active = '0'";
				fdealerrows = mydb.rawQuery(query, null);
			} catch (Exception e) {
			}
			if (fdealerrows != null) {
				if (fdealerrows.getCount() > 0) {
					totEvents = fdealerrows.getCount();
					title = new String[fdealerrows.getCount()];
					description = new String[fdealerrows.getCount()];
					reminder_date = new String[fdealerrows.getCount()];
					event_count_flag = new String[fdealerrows.getCount()];
					
					event_id = new String[fdealerrows.getCount()];
					event_alam_time = new String[fdealerrows.getCount()];
					event_url = new String[fdealerrows.getCount()];
					event_package = new String[fdealerrows.getCount()];
					event_ack_count = new String[fdealerrows.getCount()];
					eventReminderTime = new String[fdealerrows.getCount()];
					eventReminderNumber = new String[fdealerrows.getCount()];
					eventWaitTime = new String[fdealerrows.getCount()];
					eventNotifyType = new String[fdealerrows.getCount()];
					
					int eventIndexCounter = 0;
					while (fdealerrows.moveToNext()) {
						String CurrentTitle = fdealerrows.getString(3);
						resultTitle.add(CurrentTitle); 
						int  eventPrintCount = eventIndexCounter;
						eventPrintCount =  eventPrintCount + 1;
						
						title[eventIndexCounter] = fdealerrows.getString(3);
						description[eventIndexCounter] = fdealerrows.getString(4);
						reminder_date[eventIndexCounter] = fdealerrows.getString(6) + " " + fdealerrows.getString(7);
						event_count_flag[eventIndexCounter] = eventPrintCount + "  Of  " + totEvents + "  Events";

						event_id[eventIndexCounter] = fdealerrows.getString(1);
						event_alam_time[eventIndexCounter] = fdealerrows.getString(8);
						event_url[eventIndexCounter] = fdealerrows.getString(11);
						event_package[eventIndexCounter] = fdealerrows.getString(12);
						event_ack_count[eventIndexCounter] = fdealerrows.getString(15);
						eventReminderTime[eventIndexCounter] = fdealerrows.getString(16);
						eventReminderNumber[eventIndexCounter] = fdealerrows.getString(17);
						eventWaitTime[eventIndexCounter] = fdealerrows.getString(18);
						eventNotifyType[eventIndexCounter] = fdealerrows.getString(19);
						Log.v("Print @@@@@@@@@@@@@@@@@@ ", " : " + eventIndexCounter);
						eventIndexCounter = eventIndexCounter + 1;	
					}
					// event_url, event_package, event_ack_count, eventReminderTime, eventReminderNumber, eventWaitTime, eventNotifyType
					
					// Locate the ViewPager in viewpager_main.xml
					viewPager = (ViewPager) findViewById(R.id.pager);
					// Pass results to ViewPagerAdapter Class
					adapter = new AlertMessagePreviewAdapter(AlertMessagePreview.this, title, description, reminder_date, event_count_flag, event_id,event_alam_time, event_url, event_package, event_ack_count, eventReminderTime, eventReminderNumber, eventWaitTime, eventNotifyType, viewPager);
					// Binds the Adapter to the ViewPager
					viewPager.setAdapter(adapter);
				    
				    MemoryAngelPageListener pageListener = new MemoryAngelPageListener(AlertMessagePreview.this, title, description, reminder_date, event_count_flag, event_id,event_alam_time, event_url, event_package, event_ack_count, eventReminderTime, eventReminderNumber, eventWaitTime, eventNotifyType);
                    viewPager.setOnPageChangeListener(pageListener);
                    pageListener.onPageSelected(0);
				}	
			}
		}
	}


	@Override
	public void onInit(int arg0) {
		// TODO Auto-generated method stub
		
	}
}
